package com.atguigu.srb.common.exception;

import com.atguigu.srb.common.result.R;
import org.springframework.jdbc.BadSqlGrammarException;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLException;

@Component
@RestControllerAdvice
public class UnifiedExceptionHandler {

    @ExceptionHandler(Exception.class)
    public R handleException(Exception e) {
        //通用异常处理
        System.out.println(e.getMessage());
        return R.error().message(e.getMessage());
    }

//    @ExceptionHandler(SQLException.class)
//    public R handleSqlException(Exception e) {
//        //通用异常处理
//        System.out.println(e.getMessage());
//        return R.error().message(e.getMessage());
//    }
    @ExceptionHandler(BadSqlGrammarException.class)
    public R handleSqlException(BadSqlGrammarException e) {
        //jdbc的sql异常处理
        System.out.println(e.getMessage());
        return R.error().message(e.getMessage());
    }
}
